import React, { Component } from 'react'
import Loadable from 'react-loadable'
import Promise from 'promise-polyfill'
import { Route, Switch, HashRouter } from 'react-router-dom'
import './App.css'
import { UpdateStatus, store } from './rudexRou/redux.js'
import { dataAxios } from './rudexRou/request.js'
import axios from './rudexRou/urlHttp.js'
import { addDictionaryInfo } from './rudexRou/redux'
import { getLinkToUrl, devProTest } from './rudexRou/OperatesCookie.js'
if (!window.Promise) {
  window.Promise = Promise
}
const MyLoadingComponent = ({ isLoading, error }) => {
  // Handle the loading state
  if (isLoading) {
    return <div className="Myloading">Loading...</div>
  } else if (error) {
    return (
      <div className="Myloading">
        Sorry, there was a problem loading the page.
      </div>
    )
  } else {
    return null
  }
}
//主页
const SecondPages = Loadable({
  loader: () => import('./componentJsx/SecondPages/SecondPages.jsx'),
  loading: MyLoadingComponent,
})
//登录
const Login = Loadable({
  loader: () => import('./pages/Login/index.jsx'),
  loading: MyLoadingComponent,
})
class App extends Component {
  // 查询数据字典
  // queryDictionaryInfo(){
  //     axios.get('/mag-its-release/release/config/queryDictionaryInfo').then(function (response) {
  //         if(response.status === 200){
  //             if (response.data.code === 200 && response.data.data !== null) {
  //                 response.data.data.map((key, index) => {
  //                     store.dispatch(addDictionaryInfo(key.dicName, key.dicType, parseInt(key.dicValue), key.typeCnName));
  //                     UpdateStatus();
  //                 });
  //             }
  //         }
  //     }).catch(function (error) {
  //         console.info(error);
  //     });
  // }
  queryDictionaryInfo() {
    let _this = this
    dataAxios({
      url: '/release/config/queryDictionaryInfo',
      type: 'get',
    })
      .then(function(response) {
        if (response.status === 200) {
          if (response.data.code === 200 && response.data.data !== null) {
            response.data.data.map((key, index) => {
              store.dispatch(
                addDictionaryInfo(
                  key.dicName,
                  key.dicType,
                  parseInt(key.dicValue),
                  key.typeCnName
                )
              )
              UpdateStatus()
            })
          }
          _this.setState({
            captchaPath: true,
          })
        }
      })
      .catch((error) => {
        console.info(error)
      })
  }
  componentWillMount() {
    console.log('========componentWillMount==================')
    if (devProTest() == 'ipsa' || devProTest() == 'ipsa-test') {
      let token = window.localStorage.getItem('refreshToken')
      if (!token) {
        window.location.href = getLinkToUrl('iCanLogin')
      }
    }
  }

  render() {
    let token = window.localStorage.getItem('refreshToken')
    if (token) {
      return (
        <HashRouter>
          <Switch>
            <Route path="/" component={SecondPages} />
          </Switch>
        </HashRouter>
      )
    } else {
      return (
        <HashRouter>
          <Switch>
            <Route path="/" component={Login} />
          </Switch>
        </HashRouter>
      )
    }
  }
}
export default App
